package com.marketlive.entity;

import org.marketlive.entity.IPrimaryKey;

import java.io.Serializable;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

/**
 * An implementation of <code>IPrimaryKey</code> that uses a <code>String</code> as the underlying identifier.
 */
public class StringPk implements IPrimaryKey, Serializable {

    /**
     * Type of entity for which this is a primary key.
     */
    private transient String entityType;
    /**
     * Unique identifier for the entity.
     */
    private String key;

    /**
     * Default constructor. Provided for use by the persistence mechanism.
     */
    public StringPk() {

    }

    /**
     * Constructor that fully initializes a <code>StringPk</code>.
     *
     * @param entityType the type of entity for which this is a key
     * @param key         the unique identifier represented by this key
     */
    public StringPk(final String entityType, final String key) {
        this.entityType = entityType;
        this.key = key;
    }

    /**
     * {@inheritDoc}
     */
    public String getEntityType() {
        return entityType;
    }

    /**
     * Sets the name of the entity type for which this is a key.
     *
     * @param entityType the type of entity for which this is a key
     */
    public void setEntityType(final String entityType) {
        this.entityType = entityType;
    }

    /**
     * Returns the identifier part of this key as a <code>String</code>.
     *
     * @return a string representation of the identifier part of this key
     * @see EntityHome#getPk(String)
     */
    public String getAsString() {
        return key;
    }

    /**
     * Returns a representation of this key that is useful for debugging.
     * The format is <code>entityType|identifier</code>.
     *
     * @return a representation of this key that is useful for debugging
     */
    public String toString() {
        return this.getEntityType() + "|" + this.key;
    }

    /**
     * Returns true if this key is equal to the given object.
     * <code>StringPk</code>s are equal when their underlying identifiers are equal.
     *
     * @param o object to compare with this one
     * @return true if the two objects represent the same entity
     */
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof StringPk)) {
            return false;
        }

        final StringPk stringPk = (StringPk) o;

        //todo: can't use entityType unless we set it consistently.  Want to add this back
        // if (!entityType.equals(stringPk.entityType)) return false;
        if (!key.equals(stringPk.key)) {
            return false;
        }

        return true;
    }

    /**
     * Returns the hash code of this <code>StringPk</code>.
     * The hash code is the same as the hash code of the underlying identifier.
     *
     * @return the hash code of this key
     * @throws NullPointerException if <code>getAsString()</code> returns null
     */
    public int hashCode() {
        int result;
        result = key.hashCode();
        // todo: can't use entityType unless we set it consistently
//        result = 29 * result + entityType.hashCode();
        return result;
    }
}
